#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<int> buy(n);
        auto sold = buy;
        buy[0] = -prices[0];
        for (int i = 1; i < n; i++)
        {
            buy[i] = max(buy[i - 1], sold[i - 1] - prices[i]);
            sold[i] = max(buy[i - 1] + prices[i] - fee, sold[i - 1]);
        }

        return sold[n - 1];
    }
};